package cn.stormbirds.stpaylib.handler;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

import com.alibaba.fastjson.JSONException;
import com.alibaba.fastjson.JSONObject;
import com.unionpay.UPPayAssistEx;
import com.unionpay.UPQuerySEPayInfoCallback;

import cn.stormbirds.stpaylib.PayLog;
import cn.stormbirds.stpaylib.PayResult;
import cn.stormbirds.stpaylib.PaymentActivity;
import cn.stormbirds.stpaylib.STPayCallback;
import cn.stormbirds.stpaylib.STPaySDK;

import static cn.stormbirds.stpaylib.bean.STPayCodes.CHANNEL_UNIONPAY;
import static cn.stormbirds.stpaylib.bean.STPayCodes.CODE_ERROR_CLIENT;
import static cn.stormbirds.stpaylib.bean.STPayCodes.TRANSACTION_APP;

/**
 * Copyright (c) 小宝 @2019
 *
 * @ Package Name:    cn.stormbirds.stpaylib.handler
 * @ Author：         stormbirds
 * @ Email：          xbaojun@gmail.com
 * @ Created At：     2019/7/3 18:15
 * @ Description：
 */


public class UnionPayHandler extends BasePayHandler {

    public UnionPayHandler(STPayCallback callback, JSONObject preOrder) {
        super(callback, preOrder);
    }

    @Override
    public void pay(final Activity activity) {
        if (getClass().getResourceAsStream("/assets/data.bin") == null) {
            PayLog.e("缺少 data.bin 文件, 将 SDK 中 assets 下 data.bin  文件拷贝到项目中");
            this.onPayFinished(PayResult.makeResult(CODE_ERROR_CLIENT, CHANNEL_UNIONPAY,TRANSACTION_APP,"银联支付缺少data.bin文件, 将SDK中assets下data.bin文件拷贝到项目中"));
            return;
        }

        Intent intent = new Intent(activity, PaymentActivity.class);
        intent.putExtra(PaymentActivity.EXTRA,mPreOrder.toJSONString());
        activity.startActivity(intent);
    }

    @Override
    public void realPay(final Activity activity, final JSONObject preOrder) throws JSONException {
        PayLog.i(preOrder.toJSONString());
        try {
            UPPayAssistEx.getSEPayInfo(activity, new UPQuerySEPayInfoCallback() {
                @Override public void onResult(String seName, String seType, int cardNumbers,Bundle reserved) {
                    if(STPaySDK.isPermissionSEPay) { // 允许使用手机 Pay
                        UPPayAssistEx.startSEPay(activity, null, null, preOrder.getString("tn"), preOrder.getString("mode"), seType);
                    } else {
                        UPPayAssistEx.startPay(activity, null, null, preOrder.getString("tn"), preOrder.getString("mode"));
                    }
                }

                @Override public void onError(String s, String s1, String s2, String s3) {
                    UPPayAssistEx.startPay(activity, null, null, preOrder.getString("tn"), preOrder.getString("mode"));
                }
            });
        } catch (NoClassDefFoundError e) {
            e.printStackTrace();
//                        setResultAndFinish("fail", "channel_sdk_not_included:" + channel, "不支持该渠道: " + channel + "。缺少银联的 SDK。");
        }

    }

    public void onCreate(final PaymentActivity activity){

    }

    public void onNewIntent(PaymentActivity paymentActivity, Intent intent) {
        paymentActivity.setIntent(intent);
    }

    private void finish() {
        if (this.mRef.get() != null) {
            this.mRef.get().finish();
        }
    }

    public void onDestroy() {
    }


}
